var app = getApp();
var ajax = require('../../utils/ajax.js');
var util = require('../../utils/util.js');
var isPageEnd = false;
var nowPage = 1;
var timer = null;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataLoaded: false,
    nowPage: 1,
    userName: "",
    myHidden: false,
    inputValue: "",
    gpsFail: false,
    nearbyStoresList: [],
    windowHeight: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var _self = this;
    _self.getSystemInfo();
    var nowPage = _self.data.nowPage;
    // 生命周期函数--监听页面加载
    this.setData({
      loading_hidden: false,
      nowPage: 1,
      // userInfo: app.getUserData(),
    })

    var {lat, lng} = app.getLocation();
    if (!lat || !lng) {
      wx.getLocation({
        success: function (result) {
          var lat = result.latitude;
          var lng = result.longitude;
          app.setLocation(lat, lng);
          _self.getStoreList(nowPage);
        },
        fail: function (result) {
          _self.setData({
            gpsFail: true,
            loadingState: true
          })
        }
      });
    } else {
      _self.getStoreList(nowPage);
    }
  },
  onShareAppMessage: function () {
    return {
      title: this.data.shop.shareTitle,
      path: "pages/index/index"
    }
  },
  onPullDownRefresh: function () {
    this.setData({
      nowPage: 1,
      nearbyStoresList: [],
    })
    var nowPage = this.data.nowPage;
    this.getStoreList(nowPage);
    isPageEnd = false;
  },
  onReachBottom: function () {
    if (isPageEnd) {
      return;
    }
    this.setData({
      nowPage: this.data.nowPage + 1
    })
    var nowPage = this.data.nowPage;
    this.getStoreList(nowPage);

  },
  /**
   * 重新授权共享地址
   */
  _getGPS: function () {
    var _self = this;
    var nowPage = _self.data.nowPage;
    _self.setData({
      dataLoaded: false,
      gpsFail: false
    })
    wx.openSetting({
      success: function () {
        wx.getLocation({
          success: function (result) {
            var lat = result.latitude;
            var lng = result.longitude;
            app.setLocation(lat, lng);
            _self.getStoreList(nowPage);
          },
          fail: function (result) {
            _self.setData({
              gpsFail: true,
              dataLoaded: true
            })
          }
        });
      }
    })
  },
  /**
   * 跳转至地图
   */
  _toMap: function () {
    ajax.G('/openapi/CAddress/getLocation', {
      addr: this.data.nearbyStoresList.address
    }, function (data) {
      var addressInfo = JSON.parse(data.result)['result'];
      wx.openLocation({
        latitude: addressInfo.location.lat,
        longitude: addressInfo.location.lng,
        name: this.data.nearbyStoresList.title,
        address: this.data.nearbyStoresList.address,
      })
    });
  },
  /**
   * 获取商铺列表
   */
  getStoreList: function (nowPage) {
    var _self = this;
    var {lat, lng} = app.getLocation();
    ajax.G('/openapi/CHome/nearbyStoresList', {
      page: nowPage,
      lng,
      lat,
    }, function (data) {

      var o = _self.data.nearbyStoresList;
      var d = data.result['nearbyStoresList'] || [];
      d.forEach(function (stores) {
        var range = stores.juli;

        if (range < 1000) {
          range = range + ' m';
        } else {
          range = (parseFloat(range) / 1000).toFixed(2) + ' km';
        }
        stores.juli = range;

      });
      if (!d || d.length < 10) {
        isPageEnd = true;
      }
      if (d) {
        o.push(d);
      }
      _self.setData({
        nearbyStoresList: o,
        dataLoaded: true,
      })
    });
  },

  getSystemInfo: function () {
    var _self = this;
    wx.getSystemInfo({
      success: function (res) {
        _self.setData({
          windowHeight: res.windowHeight / 10 + 'px'
        })
        console.log(_self.data.windowHeight);
      }
    })
  },
})